<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/6
 * Time: 16:06
 */

namespace app\ext\controller;

use think\cache\driver\Redis;
use app\ext\model\DemoModel;
use think\Controller;
use think\Loader;

class Demo extends Controller
{
    public function index(){
        $this->assign('title','123');
        return view('index');
    }

    public function treeListLoad()
    {
        $name = input('name','');

        $this->assign('title','无限级分类异步加载');
        return view('tree',['name'=>$name]);
    }

    public function fileList()
    {
        $file_path = str_replace('\\','/',ROOT_PATH) . 'public/static/file/';

        $handle = opendir($file_path);
        while(($fl = readdir($handle)) != false)
        {
            if(!is_dir($fl) && $fl != '.' && $fl != '..' ){
                $files[] = $fl;
            }
        }

        $demoModel = new DemoModel();
        foreach($files as $key => $value)
        {
            $filename = coding($value);
            $bookList[$key]['path'] = $file_path . $filename;
            $bookList[$key]['name'] = pathinfo($filename)['filename'];
            $bookList[$key]['filename'] = $filename;
            $bookList[$key]['isLeadingIn'] = $demoModel->isLeadIn(pathinfo($filename)['filename']);
        }

        $this->assign('bookList',$demoModel->bList());
        $this->assign('title','文件列表');
        $this->assign('files',$bookList);
        return view('fileList');
    }

    public function readFile()
    {
        $file_name = input('path')?:'';
        $bid = (int)input('id')?:0;
        empty($bid) && empty($file_name) && $this->error('参数错误');
        $demoModel = new DemoModel();

        if($bid > 0){
            $bookInfo = $demoModel->bList($bid);
            if($bookInfo['is_complete'] == 0)
                $demoModel->clearB($bid);
            $file_name = $bookInfo['name'];
        }else{
            $bid = $demoModel->addB($file_name);
        }
        $file_path = str_replace('\\','/',ROOT_PATH) . 'public/static/file/'.$file_name.'.txt';
        $content = file($file_path);

        $rs = [];
        foreach($content as $key => $value)
        {
            if($value != "\r\n"){
                $rs[] = $demoModel->addBC($bid,coding($value),$key+1);
            }
        }

        $demoModel->completeB($bid);
        $this->redirect(url('ext/demo/fileContent',array('id'=>$bid)));
    }

    public function fileContent()
    {
        $id = input('id')?:1;

        $demoModel = new DemoModel();
        $book = $demoModel->bList($id);

        $rs = db('bcontent')->where(['b_id'=>$id])->order('id asc')->paginate(300);

        foreach($rs as $key => &$value)
        {
            if($value == '\"\\r\\n\"'){
                unset($value);
            }
            $value['content'] = str_replace(array('\r','\n'), "", $value['content']);
        }

        $this->assign('book',$book);
        $this->assign('title',$book['name']);
        return view('fileContent',['list'=>$rs]);
    }

    public function toPdf()
    {
        header("Content-type:text/html;charset=utf-8");
        $file = $_SERVER['DOCUMENT_ROOT'].'/static/pdf/'.time().'.pdf';
        vendor('tcpdf.tcpdf');
        $tcpdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,true, 'UTF-8', false);

//设置作者，标题，文件属性
        $tcpdf->SetCreator(PDF_CREATOR);
        $tcpdf->SetAuthor('Nicola Asuni');
        $tcpdf->SetTitle('TCPDF Example 001');
        $tcpdf->SetSubject('TCPDF Tutorial');
        $tcpdf->SetKeywords('TCPDF, PDF, example, test, guide');

// 设置页眉和页脚信息
        $tcpdf->setHeaderData('', 30, '读懂美国史', 'zeng94731的博客', array(0,64,255), array(0,64,128));
        $tcpdf->setFooterData(array(0,64,0), array(0,64,128));

// 设置页眉和页脚字体
        $tcpdf->setHeaderFont(Array('stsongstdlight', '', '10'));
        $tcpdf->setFooterFont(Array('helvetica', '', '8'));

//设置文档对齐，间距，字体，图片
        $tcpdf->SetCreator(PDF_CREATOR);
        $tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
        $tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//设置页眉页脚 边距
        $tcpdf->setHeaderMargin(PDF_MARGIN_HEADER);
        $tcpdf->setFooterMargin(PDF_MARGIN_FOOTER);

//自动分页
        $tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $tcpdf->setFontSubsetting(true);
        $tcpdf->AddPage();
        $tcpdf->setPageMark();

//设置正文字体，大小   （stsongstdlight，网上说这个字体支持的文字更全，支持中文不乱码）
        $tcpdf->SetFont('stsongstdlight', '', 10);

/*//创建页面，渲染PDF
        $tcpdf->AddPage();*/

        $html = '123';

        $tcpdf->writeHTML($html, true, false, true, true, '');
        $tcpdf->lastPage();

//PDF输出   I：在浏览器中打开，D：下载，F：在服务器生成pdf ，S：只返回pdf的字符串
        $tcpdf->Output($file,'F');
    }

    public function history()
    {
        $this->assign('title','时间轴');
        return view();
    }

    public function testredis()
    {
        $redis = new Redis();
        $redis->set('user_name','dsheldon');

        echo $redis->get('user_name');
    }

    public function getredis()
    {
        $redis = new Redis();
        echo $redis->get('user_name');
    }
}